
package com.cptravel.activity.explore;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.core.api.ApiListenrDefaultImpl;
import com.core.api.B5MBaseResponse;
import com.core.controller.B5MBaseActivity;
import com.core.util.B5MActivityHelper;
import com.core.widget.pulltorefresh.PullToRefreshBase.Mode;
import com.core.widget.pulltorefresh.PullToRefreshListView;
import com.cptravel.R;
import com.cptravel.activity.hot.HotDetailActivity;
import com.cptravel.adapter.HotAdapter;
import com.cptravel.api.GsearchResponse;
import com.cptravel.api.GsearchResquest;
import com.cptravel.api.SearchRequest;
import com.cptravel.bean.Product;

public class SearchResultActivity extends B5MBaseActivity
{
    private HotAdapter hotAdapter;
    private PullToRefreshListView mPullToRefreshListView;
    private GsearchResquest mGsearchResquest;
    private SearchRequest mSearchRequest;
    private Bundle bundle;
    private String type;

    @Override
    protected String getHeadTitle()
    {
        // TODO Auto-generated method stub
        return "搜索结果";
    }

    @Override
    public int doGetContentViewId()
    {
        // TODO Auto-generated method stub
        return R.layout.search_result;
    }

    @Override
    public void doInitSubViews(View containerView)
    {
        // TODO Auto-generated method stub
        mPullToRefreshListView = (PullToRefreshListView) containerView.findViewById(R.id.pull_refresh_list);
        mPullToRefreshListView.setMode(Mode.DISABLED);
        mPullToRefreshListView.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0 , View arg1 , int arg2 , long arg3)
            {
                // TODO Auto-generated method stub
                Product product = hotAdapter.getList().get((int) arg3);
                Bundle bundle = new Bundle();
                bundle.putSerializable("product", product);
                B5MActivityHelper.startActivity(mActivity, HotDetailActivity.class, bundle);
            }
        });
    }

    @Override
    public void doInitDataes()
    {
        // TODO Auto-generated method stub
        hotAdapter = new HotAdapter(mActivity, new ArrayList<Product>());
        bundle = getIntent().getExtras();
        type = bundle.getString("type");
        if (SearchCustomActivity.class.getName().equals(type))
        {
            //
            mSearchRequest = new SearchRequest(new ApiListenrDefaultImpl()
            {
                @Override
                public void onSuccess(B5MBaseResponse b5mBaseResponse)
                {
                    // TODO Auto-generated method stub
                    super.onSuccess(b5mBaseResponse);
                }
            }, mProgressBar);
            mSearchRequest.skey = bundle.getString("skey");
        }
        else if (SearchAdvancedActivity.class.getName().equals(type))
        {
            mGsearchResquest = new GsearchResquest(new ApiListenrDefaultImpl()
            {
                @Override
                public void onSuccess(B5MBaseResponse b5mBaseResponse)
                {
                    // TODO Auto-generated method stub
                    super.onSuccess(b5mBaseResponse);
                    GsearchResponse gsearchResponse = (GsearchResponse) b5mBaseResponse;
                    hotAdapter.setList(gsearchResponse.products);
                    hotAdapter.notifyDataSetChanged();
                }
            }, mProgressBar);
            mGsearchResquest.area = bundle.getString("area");
            mGsearchResquest.skey = bundle.getString("skey");
            mGsearchResquest.typeid = bundle.getString("typeid");
            mGsearchResquest.priceA = bundle.getString("priceA");
            mGsearchResquest.priceB = bundle.getString("priceB");
        }
    }

    @Override
    public void doAfter()
    {
        // TODO Auto-generated method stub
        if (SearchCustomActivity.class.getName().equals(type))
        {
            mSearchRequest.start();
        }
        else if (SearchAdvancedActivity.class.getName().equals(type))
        {
            mGsearchResquest.start();
        }
    }
}
